#include <iostream>
#include <thread>
#include <memory>
#include <future>
#include <functional>

int Add(int num1, int num2)
{
    return num1 + num2;
}

int main()
{
    int a = 10, b = 20;
    // 封装任务
    //std::packaged_task<int()> task(std::bind(&Add, a, b));
    auto task = std::make_shared<std::packaged_task<int()>>(std::bind(&Add, a, b));

    std::future<int> future_res = task->get_future();

    // 主线程执行其他任务
    std::cout << "hello world\n";

    /*std::cout << "++++++++++++++++++++++++++++\n";
    std::cout << future_res.get() << std::endl;*/
    //在此处调用则会一直阻塞

    // 必须手动执行任务
    //task();
    //在其他线程里运行
    std::thread th( [task]() {  (*task)();  });

    std::cout << "++++++++++++++++++++++++++++\n";
    std::cout << future_res.get() << std::endl;
    th.join();

    return 0;
}